package com.yanqu.road.server.manager.activity.sonhai.handler.impl;

import com.yanqu.road.entity.activity.sonhai.config.SonHaiEventConfig;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiBattleLogTypeEnum;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiPlotTypeEnum;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiRewardFlagEnum;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiCreatePlotResp;
import com.yanqu.road.entity.activity.sonhai.param.SonHaiWinPlotResp;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.handler.IPlotTypeHandler;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreReq;
import com.yanqu.road.server.manager.activity.sonhai.handler.entity.PlotTypeHandlerExploreResp;
import com.yanqu.road.utils.RandomHelper;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 当康巢
 */
public class PlotType10Handler implements IPlotTypeHandler {

    /**
     * 探索后处理-随机探明周边X格范围内的Y个地块，若其能升级，则将其等级提升1级
     * @param req
     * @return
     */
    @Override
    public PlotTypeHandlerExploreResp explore(PlotTypeHandlerExploreReq req) {
        CrossSonHaiUser user = req.getUser();
        SonHaiRegionData regionData = user.getRegionData();
        CrossSonHaiActivity activity = req.getActivity();
        CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
        SonHaiPlotData plotData = req.getPlotData();
        SonHaiEventConfig eventConfig = activity.getSonHaiConfig().getEventConfig(plotData.getPlotId());
        //原寻踪进度
        int oldProgress = regionData.getProgress();
        //直接镇伏该地块
        SonHaiWinPlotResp winPlotResp = user.winPlot(plotData);
        //获得积分
        long addScore = user.addScoreByPlot(regionData, plotData, oldProgress);
        winPlotResp.setAddScore(addScore);
        //添加山海灵气
        long baseAddAuraScore = winPlotResp.getAddAuraScore();
        int auraScoreOpRate = user.getAuraScoreOpRate(plotData, true);//buff:山海灵气加成率
        long auraScoreOpValue = user.getAuraScoreOpValue(plotData, true);//buff:山海灵气加成值
        long addAuraScore = user.countAureScore(baseAddAuraScore, auraScoreOpRate, auraScoreOpValue);
        if (addAuraScore > 0) {
            team.changeAuraScore(user, addAuraScore);
        }
        winPlotResp.setAddAuraScore(addAuraScore);
        //效果处理
        int range = eventConfig.getParamList().get(0);//读配置，range范围内
        int num = eventConfig.getParamList().get(1);//读配置，num个地块
        //触发当康巢地块时，额外多探明x个地块
        int extraNum = user.extraExplorePlot();
        num += extraNum;
        //随机生成地块
        List<SonHaiCreatePlotResp> plotDataList = activity.randomCreatePlot(user, regionData, plotData, range, num, false);
        //若其能升级，则将其等级提升1级
        for (SonHaiCreatePlotResp createPlotResp : plotDataList) {
            SonHaiPlotData newPlotData = createPlotResp.getPlotData();
            //读配置，判断是否能升级
            SonHaiEventConfig nextLevelEventConfig = activity.getSonHaiConfig().getNextLevelEvent(newPlotData.getPlotId());
            if (nextLevelEventConfig != null) {
                if (newPlotData.getType() == SonHaiPlotTypeEnum.PLOT_TYPE_13.getType()) {
                    //旧蛋
                    SonHaiEggData oldEgg = activity.getEgg(newPlotData.getEggId());
                    //是蛋，判断一下是否能升级
                    PlotType13Handler plotType13Handler = new PlotType13Handler();
                    SonHaiEggData eggData = plotType13Handler.createEgg(activity, team, user, nextLevelEventConfig.getId(), newPlotData);
                    if (eggData != null) {
                        //旧蛋移除监听地块
                        oldEgg.removeAttackMeUserPlot(newPlotData.getUserId());
                        //地块注入新蛋
                        newPlotData.setPlotId(nextLevelEventConfig.getId());
                        newPlotData.setLevel(nextLevelEventConfig.getLevel());
                    }
                } else {
                    //不是蛋，直接升级
                    newPlotData.setPlotId(nextLevelEventConfig.getId());
                    newPlotData.setLevel(nextLevelEventConfig.getLevel());
                }
            }
        }
        //返回
        PlotTypeHandlerExploreResp resp = new PlotTypeHandlerExploreResp();
        resp.setNewPlotDataList(plotDataList);
        resp.setWinPlotResp(winPlotResp);
        return resp;
    }
}
